package emulator.hardware.nmos6502.commands;

import emulator.EmulatorException;
import emulator.hardware.HwByte;
import emulator.hardware.nmos6502.Command;
import emulator.hardware.nmos6502.CommandSet;
import emulator.hardware.nmos6502.Cpu6502;
import emulator.hardware.nmos6502.Flags6502;
import emulator.hardware.nmos6502.Operand;
import emulator.hardware.nmos6502.operands.Immediate;

/**	ANC    ***
 *	ANC ANDs the contents of the A register with an immediate value and then 
 *	moves bit 7 of A into the Carry flag.  This opcode works basically 
 *	identically to AND #immed. except that the Carry flag is set to the same 
 *	state that the Negative flag is set to.
 *	
 *	One supported mode:
 *	
 *	ANC #ab         ;2B ab       ;No. Cycles= 2
 *	ANC #ab         ;0B ab
 */

public class ANC implements Command {

	@Override
	public void execute(Cpu6502 cpu, Operand operand) throws EmulatorException {
		int accu_old = (int)cpu.getAccu().getNumber();
		Flags6502 flags = cpu.getFlags();
		
		int accu = accu_old & (int)operand.getByte().getNumber();
		
		flags.setNZFromValue(accu);
		flags.setCarry(flags.getNegative());
		
		cpu.setAccu(new HwByte(accu));
		cpu.setFlags(flags);
	}

	@Override
	public String getName() {
		return "ANC";
	}

	@Override
	public void register(CommandSet cset) {
		cset.defineCommand(0x2B, this, new Immediate());
		cset.defineCommand(0x0B, this, new Immediate());
	}

}
